const { exec } = require("child_process");
const curl =
  
// start();
function start() {
  // setTimeout(() => {
  exec(curl, (err, stdout, stderr) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(`stderr: ${stderr}`);
    if (stdout) {
      const res = JSON.parse(stdout);
      const rooms = res.Data.Rows.filter((e) => {
        return (
          ((e.RoomTypeName === "1室0厅1卫" && e.MonthlyRent < 2100) ||
            (e.RoomTypeName === "1室1厅1卫" && e.MonthlyRent < 2300) ||
            (e.RoomTypeName === "2室1厅1卫" && e.MonthlyRent < 3200) ||
            (e.RoomTypeName === "2室0厅1卫" && e.MonthlyRent < 3200)) &&
          e.peocount < 3
        );
      });
      if (rooms.length === 0) {
        start();
      }
      send(rooms);
    }
  });
  // }, (Math.floor(Math.random() * 10) + 10) * 60 * 1000);
}

async function send(rooms) {
  console.log("🚀 ~ file: request.js:58 ~ send ~ rooms:", rooms);
  const nodemailer = require("nodemailer");

  let transporter = nodemailer.createTransport({
    host: "smtp.163.com",
    secure: true,
    secureConnection: true, // 使用了 SSL
    auth: {
      user: "awxy115@163.com",
      pass: "QDEODRASHRIELOPC",
    },
  });

  let mailOptions = {
    from: "awxy115@163.com", // sender address
    to: "awxy115@163.com", // list of receivers
    subject: "有新的room", // Subject line
    // text: 'Hello world?', // plain text body
    html: "<b>Hello world?</b>", // html body
  };
  const html = rooms.map((e) => {
    return `<h4>小区名称：${e.PropertyName}</h4>
    <h4>楼层${e.FloorName}</h4>
    <h4>房间类型${e.RoomTypeName}</h4>
    <h4>已抢人数${e.peocount}</h4>
    <h4>开抢时间${e.begdate} --- ${e.enddate}</h4>
    `;
  });
  mailOptions.html = html.join("\n------------------------------------");
  try {
    await transporter.sendMail(mailOptions);
  } catch (err) {
    console.log("🚀 ~ file: request.js:90 ~ send ~ err:", err);
  }
}

function getUrl(){
  "curl 'https://www.sjgzf.cn/api/api/PStruct/QueryPRoomLst' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Cookie: SECKEY_ABVK=tmiTi7qQp1yCRHgnJFwOmnlG9RRFTeB2w0pGYtgEaSc%3D; BMAP_SECKEY=vKnhfPmVDKxShtIj1CQDqlLWTsfpZdT_OElWPvdxrZZ4SneAJ_aFZpNjKPsuwxyRGHolfAg-TpOZp7wcKv3I3XKMyRNWTu-Wc9it9oFSmk4PH61Wr8bEwhkMasvZqx8Kv50bG_mnlhPLwueMJlibgVbMRzDRfndEabNOB48PMVnp61HXxrKpiNqI0n6phOmL; ShangHaiKuangcang_LoginUserKey=27CF9DAC4962C155FA5775F6597184221F3DC6A88B0A5DFE1F0D108964411A14011B210609458F3B709B616647076715E487733E24F81DE47AEB2ED7A2C9BE3E8E067CEB7FA6A96ECCD2BA717770B9D580F370576B4489582CDD9FB39E6B4621A7B18E001799932392BD993D96C287C639E0261EA77ABF1232343F1151722D20B65435A180DE02FC0F074B7780E871A2AA85B9E57FEEB405C330D661180FD4FC0D941DCD33EAAA806EB5FCF8260A94662F685F96E7D8B00F12C1C4A7D30F86D95E42B390F4D213479B45E693DB899210352CE704BE22BB8C1954AB3C41373CDA12F353B4DB2998919C40D7989EBEB005B4F2C60C8838CAE5316D0835452E1F9A5640E6A1E5C622E080110DC1058B864FA181BD69C09DC0ADEF5E18A4F776D345C02A69D6F12CB27D05EC050941C6EA5B3E3965818EBF5977E2E530A8E751A604B7FD8FBEA8BD065F7379838AE517DB744C1DDA607999D1B1D8190588AD0E9FADD7C866CFE9F88983791F72FC2499D170F3C4E523231B9ACF485C03A52F9CBFA138D12000A7255D727954FDB75535B2C8928905852A0713E8E10DBAFE019E6BD7B41EE2BA2B612F341D66E9527B50D442767BDA9B02B78D59F16D1657359B00D0708FA89431428569' \
  -H 'Origin: https://www.sjgzf.cn' \
  -H 'Pragma: no-cache' \
  -H 'Referer: https://www.sjgzf.cn/houseLists' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-origin' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36' \
  -H 'nonce: I8TF16IPW5ELCCAKBKVWR7WEMSR78ZHU' \
  -H 'sec-ch-ua: \"Google Chrome\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: \"macOS\"' \
  -H 'signature: 15KBE8UGZYVI7LJI137X5PX4N43TX1QR' \
  -H 'timestamp: 1I6ZYKOS1CXQ197Y5UZX89DINYGH7DHN' \
  -H 'token: Q7RYMPPYEPHCIADUP5S2MOSJR16KHU4XCT' \
  --data-raw 'QueryJson%5BType%5D=5&QueryJson%5BKeyWord%5D=&QueryJson%5BAreaId%5D=310117002&QueryJson%5BPropertyId%5D=&QueryJson%5BRoomTypeName%5D=&QueryJson%5BRental%5D=&Page=1&Rows=10' \
  --compressed";
}
